package com.cmcc.linlaker.ui.mine;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.cmcc.linlaker.LoginActivity;
import com.cmcc.linlaker.R;
import com.cmcc.linlaker.bean.UpdateBean;
import com.cmcc.linlaker.http.ConfigManager;
import com.cmcc.linlaker.utils.BarUtils;
import com.cmcc.linlaker.utils.ToastUtils;
import com.cmcc.linlaker.utils.UpdateManager;
import com.cmcc.linlaker.utils.Utils;
import com.cmcc.linlaker.view.UpdateDialog;
import com.cmcc.linlaker.volley.GsonRequestHelper;

import java.util.HashMap;
import java.util.Map;

public class AboutUsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_us);

        BarUtils.initStatusBar(this);


        initView();
    }

    private TextView text_version, text_update_info;

    private void initView() {
        text_version = findViewById(R.id.text_version);
        text_version.setText("Version " + Utils.getAppVersionName(this));

        text_update_info = findViewById(R.id.text_update_info);
        checkUpdate();
    }

    public void back(View view) {
        finish();
    }


    private UpdateBean updateBean;

    /**
     * 检测更新
     */
    private void checkUpdate() {
        GsonRequestHelper helper = new GsonRequestHelper(this);
        helper.sendGETRequest(ConfigManager.updateUrl, UpdateBean.class, false, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    UpdateBean bean = (UpdateBean) data;
                    dealUpgrade(bean);
                }
            }
        });
    }

    private boolean isCanUpdate = false;

    private void dealUpgrade(final UpdateBean bean) {
        updateBean = bean;
        try {
            int versionCodeService = Integer.parseInt(bean.getVersion());
            PackageInfo pi = getPackageManager().getPackageInfo(getPackageName(), 0);
            int versionCodeLocal = pi.versionCode;

            if (versionCodeLocal < versionCodeService) {
                // 比服务器上的版本低 需要更新
                isCanUpdate = true;
                text_update_info.setText("有新版本 V" + bean.getVersionShort());
            }

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void doUpdate(View view) {
        if (isCanUpdate) {
            // 比服务器上的版本低 需要更新
            showUpdateDialog(updateBean);
        } else {
            ToastUtils.showText(AboutUsActivity.this, "当前已是最新版本");
        }
    }

    private void showUpdateDialog(UpdateBean bean) {
        final UpdateDialog dialog = new UpdateDialog(this, bean, R.style.CustomDialog);
        dialog.setNewVersionListener(new UpdateDialog.OnNewVersionListener() {
            @Override
            public void update() {
                dialog.dismiss();
                installApk();
            }
        });
        dialog.setOnDialogCancelListener(new UpdateDialog.OnDialogCancelListener() {
            @Override
            public void cancel() {
                dialog.dismiss();
            }
        });
        dialog.setCancelable(false);
        dialog.show();
    }

    /**
     * 下载apk  并获取安装权限
     */
    public void installApk() {
        UpdateManager updateManager = new UpdateManager(this, updateBean.getInstall_url());
        updateManager.showDownloadDialog();
    }

}
